<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      住房管理
    </div>
    <div style="margin-left: 400PX">
      <el-select v-model="state" placeholder="房间状态"
                 style="width: 200px;margin-left: 40px" clearable>
        <el-option
          v-for="item in stateList"
          :key="item.label"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-input v-model="roomNumber" placeholder="房间号"
                style="width: 200px;margin-left: 40px" clearable/>
      <el-button type="primary" @click="selectAll"
                 style="margin-left: 40px">查询
      </el-button>
      <el-button style="margin-left: 40px" @click="check">重置</el-button>
    </div>
    <div style="margin-top: 20px">
      <el-table :data="selectList" height="659px" border style="width: 100%">
        <el-table-column align="center" fixed prop="roomId" sortable label="房间ID"/>
        <el-table-column align="center" fixed prop="roomNumber" sortable label="房间号"/>
        <el-table-column align="center" fixed prop="state" label="房间状态">
          <template v-slot="scope">
            <div :style="{ color: scope.row.state === '空闲' ? 'green' : 'red' }">
              {{ scope.row.state }}
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" fixed prop="remark" sortable label="房间备注"/>
        <el-table-column align="center" fixed prop="roomTypeName" sortable label="房间类型"/>
        <el-table-column align="center" fixed prop="roomPrice" sortable label="房间价格"/>
        <el-table-column align="center" fixed prop="customerName" sortable label="顾客名称"/>
        <el-table-column align="center" fixed prop="identificationCard" sortable label="身份证号"/>
        <el-table-column align="center" fixed="right" label="操作">
          <template v-slot="scope">
            <!--传入的scope.row参数为当前行的数据-->
            <el-button v-if="scope.row.state === '空闲' " @click="insertStayButton(scope.row)" type="text" size="small">入住</el-button>
            <el-button v-if="scope.row.state === '已入住' " @click="cancelStayButton(scope.row)" type="text" size="small">退房</el-button>
            <el-button v-if="scope.row.state === '已入住' " @click="abrogateStayButton(scope.row)" type="text" size="small">作废</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <!--新增弹窗-->
      <el-dialog title="办理入住" width="40%" :visible.sync="insertStayButtonController" @close="closeDialog('ruleForm')">
        <div style="margin: 10px">
          <el-form label-width="20%" :rules="Roles" :model="insertList" ref="ruleForm">
            <el-form-item label="房间号" prop="roomNumber">
              <el-input v-model="insertList.roomNumber" show-word-limit style="width: 80%" disabled/>
            </el-form-item>

            <el-form-item label="顾客" prop="customerId">
              <el-select show-word-limit style="width: 80%" v-model="insertList.customerId" placeholder="请选择顾客" clearable>
                <el-option
                  v-for="item in customerList"
                  :key="item.customerId"
                  :label="item.customerName + '-' + item.identificationCard"
                  :value="item.customerId"
                />
              </el-select>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
              <el-input v-model="insertList.remark" maxlength="20" show-word-limit style="width: 80%"
                        clearable placeholder="请输入备注"/>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="insertStay('ruleForm')">确定</el-button>
        <el-button @click="closeDialog('ruleForm')">取消</el-button>
      </span>
      </el-dialog>

      <el-dialog title="退房" width="40%" :visible.sync="cancelStayButtonController">
        <div style="margin: 10px">
          <H1>请确认是否退房</H1>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancelStay">确定</el-button>
        <el-button @click="cancelStayButtonController = false">取消</el-button>
      </span>
      </el-dialog>

      <el-dialog title="作废" width="40%" :visible.sync="abrogateStayButtonController">
        <div style="margin: 10px">
          <H1>请确认是否作废</H1>
        </div>
        <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="abrogateStay">确定</el-button>
        <el-button @click="abrogateStayButtonController = false">取消</el-button>
      </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { queryAll } from '@/api/customer'
import { abrogateStay, cancelStay, insertStay, selectStay } from '@/api/stay'

export default {
  name: 'stay',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  // 页面参数
  data() {
    return {
      stateList: [
        {label: '空闲', value: '空闲'},
        {label: '已入住', value: '已入住'}
      ],
      state: '',
      roomNumber: '',
      selectList: [],
      customerList: [],
      liveId: '',
      liveTableId: '',
      insertStayButtonController: false,
      cancelStayButtonController: false,
      abrogateStayButtonController: false,
      insertList: {
        customerId: '',
        roomId: '',
        roomNumber: '',
        remark: '',
      },
      Roles: {
        customerId: [
          { required: true, trigger: 'blur', message: '顾客不能为空' },
        ]
      }
    }
  },
  // 页面初始化加载方法
  mounted() {
    this.selectAll()
  },
  // 页面内事件和方法
  methods: {
    selectAll() {
      selectStay({
        state: this.state,
        roomNumber: this.roomNumber
      }).then(
        response => {
          this.selectList = response.data
        }
      )
    },
    check() {
      this.state = ''
      this.roomNumber = ''
      this.selectAll()
    },
    async selectCustomer(){
      await queryAll({}).then(
        response => {
          this.customerList = response.data
        }
      )
    },
    async insertStayButton(row) {
      await this.selectCustomer()
      this.insertList = {
        customerId: '',
        roomId: row.roomId,
        roomNumber: row.roomNumber,
        remark: '',
      },
        this.insertStayButtonController = true
    },
    insertStay(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          insertStay({
            roomId: this.insertList.roomId,
            customerId: this.insertList.customerId,
            remark: this.insertList.remark
          }).then(
            async response => {
              this.selectAll()
              this.insertStayButtonController = false
            }
          )
        } else {
          return false
        }
      })
    },
    closeDialog(formName) {
      this.$refs[formName].resetFields()
      this.insertStayButtonController = false
    },
    cancelStayButton(row) {
      this.liveId = row.liveId
      this.liveTableId = row.liveTableId
      this.cancelStayButtonController = true
    },
    abrogateStayButton(row) {
      this.liveId = row.liveId
      this.liveTableId = row.liveTableId
      this.abrogateStayButtonController = true
    },
    cancelStay() {
      cancelStay({
        liveId: this.liveId,
        liveTableId: this.liveTableId
      }).then(
        async response => {
          this.selectAll()
          this.cancelStayButtonController = false
        }
      )
    },
    abrogateStay() {
      abrogateStay({
        liveId: this.liveId,
        liveTableId: this.liveTableId
      }).then(
        async response => {
          this.selectAll()
          this.abrogateStayButtonController = false
        }
      )
    },
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
